package topin.facecat.FaceCat.UI

import facecat.topin.service.FCClientService
import topin.facecat.FaceCat.Service.HistoryServiceClient
import topin.facecat.FaceCat.Service.LatestServiceClient
import topin.facecat.FaceCat.Service.PriceDataServiceClient
import topin.facecat.FaceCat.Service.SecurityService

object DataCenter {
    /*
     * 文件分隔符号
     */
    var m_seperator = "/" //Linux
    val appPath: String
        //public static String m_seperator = "\\"; //Windows
        get() = System.getProperty("user.dir").replace("\\", "/") //Linux

    //return System.getProperty("user.dir"); //Windows
    var m_mainIP = "110.42.188.197"
    var m_mainPort = 9956
    var m_mainKID = -1
    var m_securityService: SecurityService? = null
    var m_latestServiceClient: LatestServiceClient? = null
    var m_historyServiceClient: HistoryServiceClient? = null
    var m_priceDataServiceClient: PriceDataServiceClient? = null
    fun startService() {
        m_securityService = SecurityService()
        m_latestServiceClient = LatestServiceClient()
        FCClientService.addService(m_latestServiceClient)
        m_historyServiceClient = HistoryServiceClient()
        FCClientService.addService(m_historyServiceClient)
        m_priceDataServiceClient = PriceDataServiceClient()
        FCClientService.addService(m_priceDataServiceClient)
    }

    fun connect(ip: String?, port: Int, token: ByteArray?): Int {
        return FCClientService.connectToServer(0, ip, port, "", 0, "", "", "", 6, token)
    }

    fun startConnect() {
        val socketID = connect(m_mainIP, m_mainPort, byteArrayOf(0, 0, 0, 0))
        if (socketID != -1) {
            m_latestServiceClient!!.socketID = socketID
            m_historyServiceClient!!.socketID = socketID
            m_priceDataServiceClient!!.socketID = socketID
            m_latestServiceClient!!.m_connected = true
            m_historyServiceClient!!.reqDevideDatas(FCClientService.getRequestID())
            m_historyServiceClient!!.reqCodes(FCClientService.getRequestID())
            if (socketID != -1) {
                var tick = 1000
                while (!m_securityService!!.isLoaded) {
                    try {
                        Thread.sleep(1)
                    } catch (ex: Exception) {
                    }
                    tick--
                    if (tick < 0) {
                        break
                    }
                }
            }
        }
    }
}


